/*
 * dialog widget
 */
Qlsolution.widgets.dialog = {

	createDialog: function(settings){
		$('body .widgetDialog').remove();
		var dialogContent = '';
		switch(settings.type){
			case 'form':
				$('body').append('<div class="widgetDialog" id="' + settings.idElement.substring(1) + '" title="' + settings.title + '"><p class="errors"></p><form></form></div>');
				for(val in settings.inputFields){
					if (settings.inputFields[val].type != 'hidden') {
						dialogContent += '<p class="inp"><label>' + settings.inputFields[val].label + ':</label><input type="' + settings.inputFields[val].type + '" name="' + settings.inputFields[val].name + '" class="' + settings.inputFields[val].validate + ' br3" /></p>';
					}
					else{
						dialogContent += '<input type="hidden" name="' + settings.inputFields[val].name + '" value="' + settings.inputFields[val].value + '" />';
					}
				}
				$(settings.idElement + ' form').append(dialogContent);
				break;
		}
	},

	configureDialog: function(settings){
		$(settings.idElement).dialog({
			autoOpen: settings.autoOpen,
			height: settings.height,
			width: settings.width,
			modal: true,
			buttons: {
				'Save': function(){
					settings.self = this;
					Qlsolution.widgets.dialog.saveData(settings);
				},
				'Cancel': function(){
					$(this).dialog('close');
				}
			}
		});
		$(settings.idElement).dialog('open');
	},

	saveData: function(settings){
		var paramsData = new Object();
		$(settings.idElement + ' input').each(function(key, val){
			paramsData[$(this).attr('name')] = $(this).val();
		});
		$.ajax({
			type: 'post',
			url: settings.storeUrl,
			data: 'params=' + $.toJSON(paramsData),
			success: function(msg){
				if(msg){
					Qlsolution.widgets[settings.refreshWidgetType].load(settings.refreshWidgetSettings);
				}
				$(settings.self).dialog('close');
			}
		});
	},

	start: function(settings){
		Qlsolution.widgets.dialog.createDialog(settings);
		Qlsolution.widgets.dialog.configureDialog(settings);
	}

};